package com.neusoft.a132wxyorderingdish.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.neusoft.a132wxyorderingdish.R;
import com.neusoft.a132wxyorderingdish.beans.RegisterBean;
import com.neusoft.a132wxyorderingdish.listener.RetrofitListener;
import com.neusoft.a132wxyorderingdish.model.UserModel;

public class RegisterActivity extends BaseActivity implements RetrofitListener<RegisterBean> {

    private Intent intent;
    private ImageView reg_return;
    private Button register;
    private EditText et_reg_user,et_reg_password,et_reg_repassword,
            et_reg_telephone,et_reg_address,et_reg_comment;
    private UserModel userModel;


    @SuppressLint("WrongViewCast")
    void initViews() {
        setLayout_file(R.layout.activity_register);
        register=(Button)findViewById(R.id.register);
        et_reg_user=(EditText)findViewById(R.id.et_reg_user);
        et_reg_password=(EditText)findViewById(R.id.et_reg_password);
        et_reg_repassword=(EditText)findViewById(R.id.et_reg_repassword);
        et_reg_telephone=(EditText)findViewById(R.id.et_reg_telephone);
        et_reg_address=(EditText)findViewById(R.id.et_reg_address);
        et_reg_comment=(EditText)findViewById(R.id.et_reg_comment);
        reg_return= (ImageView) findViewById(R.id.reg_return);

    }
    @Override
    void initData(){

    }


    void initEvents() {
//处理注册按钮
        register.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if(et_reg_password.getText().toString().
                        equals(et_reg_repassword.getText().toString())){
                    register();
                }
                else {
                    showToast("请确认两次密码相同");
                }
            }
        });

//处理返回按钮
        reg_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent();
                intent.putExtra("user", et_reg_user.getText().toString());
                setResult(RESULT_OK, intent);
                finish();
            }

        });

    }

    private void register(){
        userModel=new UserModel();
        userModel.register(et_reg_user.getText().toString(),
                et_reg_password.getText().toString(),
                et_reg_telephone.getText().toString(),
                et_reg_address.getText().toString(),
                et_reg_comment.getText().toString(),this);
    }





    public void onSuccess(RegisterBean registerBean, int flag) {
        if(registerBean.getSuccess().equals("1")){
            intent=new Intent();
            intent.putExtra("user",et_reg_user.getText().toString());
            setResult(RESULT_OK,intent);
            finish();
            showToast("注册成功");
        }
            else{
                showToast("注册失败");
            }

    }

    @Override
    public void onFail() {
        showToast("网络发生错误");

    }
}
